package com.shi.yibanjia.controller;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.shi.yibanjia.common.Result;
import com.shi.yibanjia.entity.po.Account;
import com.shi.yibanjia.entity.po.Mail;
import com.shi.yibanjia.exception.BusinessException;
import com.shi.yibanjia.util.MailUtil;
import com.shi.yibanjia.util.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;

import static com.shi.yibanjia.constants.RedisConstant.*;

@Api(tags = "邮箱验证码功能")
@RestController
@RequestMapping("/admin/email")
@Valid
public class EmailController {

    @ApiOperation("忘记密码-邮箱验证码")
    @GetMapping("/forgetPassword")
    public Result sendEmail4ForgetPassword(@NotBlank String email){
        // 检查邮箱是否存在
        Account one = Db.lambdaQuery(Account.class).eq(Account::getEmail, email).one();
        if (one==null){
            throw new BusinessException("该邮箱下没有绑定账号");
        }
        String code = RandomUtil.randomNumbers(4);
        RedisUtil.set(EMAIL_Check_CODE_FORGET_PASSWORD+email,code,CHECK_CODE_TTL);
        Mail mail = new Mail();
        mail.setRecipient(email);
        mail.setSubject("忘记密码");
        mail.setContent(String.format("""
                你的邮箱验证码为%s，请妥善保管，不要泄露给他人！
                """,code));
        MailUtil.sendSimpleMail(mail);
        return Result.ok();
    }

    @ApiOperation("绑定邮箱-邮箱验证码")
    @GetMapping("/bindEmail")
    public Result sendEmail4BindEmail(@NotBlank String email){
        String code = RandomUtil.randomNumbers(4);
        RedisUtil.set(EMAIL_CHECK_CODE_UPDATE_BIND_EMAIL+email,code,CHECK_CODE_TTL);
        Mail mail = new Mail();
        mail.setRecipient(email);
        mail.setSubject("绑定邮箱");
        mail.setContent(String.format("""
                你的邮箱验证码为%s，请妥善保管，不要泄露给他人！
                """,code));
        MailUtil.sendSimpleMail(mail);
        return Result.ok();
    }

    @ApiOperation("邮箱登录-邮箱验证码")
    @GetMapping("/login")
    public Result sendEmail4EmailLogin(@NotBlank String email, @ApiParam(hidden = true) HttpSession session){
        // 检查邮箱是否存在
        Account one = Db.lambdaQuery(Account.class).eq(Account::getEmail, email).one();
        if (one==null){
            throw new BusinessException("该邮箱下没有绑定账号");
        }
        String code = RandomUtil.randomNumbers(4);
        RedisUtil.set(LOGIN_CHECK_CODE_EMAIL+email,code,CHECK_CODE_TTL);
        Mail mail = new Mail();
        mail.setRecipient(email);
        mail.setSubject("登录");
        mail.setContent(String.format("""
                你的邮箱验证码为%s，请妥善保管，不要泄露给他人！
                """,code));
        MailUtil.sendSimpleMail(mail);
        return Result.ok();
    }
}
